// main/discover/discoverhome/discoverhome.js
const { $Message } = require('../../../dist/base/index');
let app = getApp()
let utils = app.utils

Page({

    /**
     * 页面的初始数据
     */
    data: {
        current:0,
        hasRefesh:true,
        list:[],
        isLoading:false,
        hotList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        that.onPullDownRefresh()
    },


    clicTopTagImg: function(evet){
        console.log(evet.currentTarget.dataset)
        var that = this;
        that.setData({ current: evet.currentTarget.dataset.number })

        
        this.onPullDownRefresh()
        
    },

    clicCellTag: function (evet){
        if (app.globalData.isLogin){
            console.log(evet.currentTarget.dataset)
            let str = JSON.stringify(evet.currentTarget.dataset);
            wx.navigateTo({
                url: '../groupdetails/groupdetails?jsonStr=' + str
            })
        }else{
            $Message({
                content: '用户未登录',
                type: 'error'
            });
        }
        
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function (data) {
        var that = this;
        that.setData({ isLoading: true })
        console.log("onPullDownRefresh")
        var list = []
        var currentTag = that.data.hotList[that.data.current]
        if (currentTag){
            if (currentTag.title != '全部') {
                data = { "tags": [currentTag.title] }
            }
        }
        
        setTimeout(function () {

            app.request.discoverHomeTop().then(res => {
                that.setData({ hotList: res.data })
            }).catch(e => {
                console.error(e)
            })

            app.request.discoverHomeList(data).then(res => {
                console.log(res)
                
                setTimeout(function () {
                    wx.hideNavigationBarLoading() //完成停止加载
                    wx.stopPullDownRefresh() //停止下拉刷新
                    that.setData({ isLoading: false })
                }, 200) //延迟时间 这里是1秒
                for (var i = 0; i < res.data.results.length; i++) {
                    var rt = res.data.results[i];
                    var groupTags = [];
                    for (var j = 0; j < rt.groupTags.length; j++) {
                        var tag = rt.groupTags[j]
                        var t;
                        if (tag == "运动") {
                            t = {
                                name: '运动',
                                checked: true,
                                color: 'yd'
                            }
                        } else if (tag == "游戏") {
                            t = {
                                name: '游戏',
                                checked: true,
                                color: 'lightBlue'
                            }
                        } else if (tag == "生活") {
                            t = {
                                name: '生活',
                                checked: true,
                                color: 'default'
                            }

                        } else if (tag == "热聊") {
                            t = {
                                name: '热聊',
                                checked: true,
                                color: 'pink'
                            }

                        }
                        groupTags.push(t);
                    }
                    rt.groupTags = groupTags;
                }



                that.setData({ list: res.data.results })
                
            }).catch(e => {
                console.error(e)
                setTimeout(function () {
                    wx.hideNavigationBarLoading() //完成停止加载
                    wx.stopPullDownRefresh() //停止下拉刷新
                    that.setData({ isLoading: false })
                }, 200) //延迟时间 这里是1秒
            })


        }, 200) 
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        console.log("onReachBottom")
        that.setData({ isLoading: true })

        setTimeout(function () {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
            that.setData({ isLoading:false })
        }, 1000) //延迟时间 这里是1秒
        
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        return {
            title: '附近密秘',
            imageUrl: 'http://pdqnh882x.bkt.clouddn.com/qun.png'
        };
    },
})